package com.flannery.learningandroid.sample.mail

import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.flannery.learningandroid.R

import kotlinx.android.synthetic.main.activity_mail_input.*

/**
 * https://blog.51cto.com/mikewang/871765
 */
class MailInputActivity : AppCompatActivity() {

    var list = ArrayList<String>()


    inner class MyTextWatcher() : TextWatcher {

        var mEt: EditText? = null
            get() {
                return field
            }
            set(value) {
                field = value
            }

        override fun afterTextChanged(s: Editable?) {
            if (mEt != null) {
                //setEditTextWidth(mEt!!, getTextWidth(mEt!!.text.toString()))
            }
        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }

    }


    var textWatcher = MyTextWatcher()

//        object : TextWatcher {
//        override fun afterTextChanged(s: Editable?) {
//            Log.i("MailInputActivity", "afterTextChanged${s}")
//            //mRecyclerView.adapter?.notifyDataSetChanged()
//        }
//
//        override fun beforeTextChanged(
//            s: CharSequence?,
//            start: Int,
//            count: Int,
//            after: Int
//        ) {
//            Log.i("MailInputActivity", "beforeTextChanged")
//            //list.set(position, s.toString())
//
//        }
//
//        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//
//            Log.i("MailInputActivity", "onTextChanged${s}")
//
//        }
//
//    }

    override
    fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mail_input)
        initData()
        mRecyclerView.layoutManager = FlowLayoutManager()
        mRecyclerView.adapter = MyAdapter()

    }

    fun initData() {
        list.add("河北省（只含省级）")
        list.add("济宁")
    }


    inner class MyAdapter : RecyclerView.Adapter<VH>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
            return VH(
                LayoutInflater.from(this@MailInputActivity)
                    .inflate(R.layout.item_mailinput, parent, false)
            )
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: VH, position: Int) {

//            holder.mEt.removeTextChangedListener()
            var str = list.get(position)
            holder.mEt.setText(str)


            //holder.mEt.movementMethod = ScrollingMovementMethod.getInstance()

            holder.mEt.removeTextChangedListener(textWatcher)
            textWatcher.mEt = holder.mEt
            holder.mEt.addTextChangedListener(textWatcher)


//            holder.mEt.addTextChangedListener(value)


        }

    }

    fun setEditTextWidth(mEt: EditText, width: Int) {
        var layoutParams = mEt.layoutParams
        mEt.layoutParams =
            LinearLayout.LayoutParams(width, layoutParams.height)
    }

    fun getTextWidth(str: String): Int {
/*
var textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
textPaint.textSize = 12F
textPaint.setColor(Color.WHITE)

var fontMetric = textPaint.getFontMetrics()
var text: String = str
*/

        var textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        textPaint.textSize = 90F
        textPaint.setColor(Color.WHITE)
        var rect: Rect = Rect()
        textPaint.getTextBounds(str, 0, str.length, rect)
        return rect.width()

    }

    inner class VH(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var mEt: EditText = itemView.findViewById(R.id.mEt)
    }
}
